package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;

import model.Perspective;
import controller.CommandManager;
import controller.TranslateCommand;
import controller.ZoomCommand;

@SuppressWarnings("serial")
public class PictureView extends BaseView implements MouseMotionListener, MouseWheelListener {
	
	// Constructor
	public PictureView(Perspective p) {
		perspective = p;
	}

	// operations
	protected void init() {
		this.addMouseMotionListener(this);
		this.addMouseWheelListener(this);
		this.setBackground(Color.BLACK);
	}

	@Override
	protected void update(Perspective p) {
		repaint();
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		BufferedImage img = perspective.getRawImage().getImage();
		g.drawImage(img, perspective.getX(), perspective.getY(), (int) (perspective.getRawImage().getWidth() * perspective.getZoom()), (int) (perspective.getRawImage().getHeight() * perspective.getZoom()), null);
		
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent arg0) {
		int notches = arg0.getWheelRotation();
		if (notches < 0) {
			CommandManager.getInstance().executeCommand(new ZoomCommand(perspective, perspective.getZoom() + 0.1f));
		} else {
			if (perspective.getZoom() == 0)
				return;
			CommandManager.getInstance().executeCommand(new ZoomCommand(perspective, perspective.getZoom() - 0.1f));
		}
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		CommandManager.getInstance().executeCommand(new TranslateCommand(perspective, (int)(arg0.getX() - (perspective.getRawImage().getWidth() * perspective.getZoom() / 2)), (int)(arg0.getY() - (perspective.getRawImage().getWidth() * perspective.getZoom() / 2))));
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}
